"""
Case Type   : cm_agent参数
Case Name   : 修改参数agent_heartbeat_timeout值为非法值（上、下边界、特殊字符、文本类型）
Create At   : 2025/4/9
Owner       : ningyali
Description :
    1.查询参数agent_heartbeat_timeout默认值
    2.修改参数值为1
    3.修改参数值为2147483648
    4.修改参数值为空
    5.修改参数值为null
Expect      :
    1.执行成功
    2.合理报错
    3.合理报错
    4.合理报错
    5.合理报错
History     :
"""

import os
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CMServer(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'),
                           Node('Standby2DbUser'))
        self.common = Common()

        text = '----prestep1:安装cm;expect:执行成功----'
        self.log.info(text)
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.pri_sh.node.ssh_user)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')

        res = self.cm.cm_install(self.user_nodes[0])
        self.assertIn('Install CM tool success', res, f'执行失败: {text}')

    def test_trans(self):
        text = '----step1:查询参数agent_heartbeat_timeout默认值;expect:执行成功----'
        self.log.info(text)
        q_logdir_cmd = f"source {macro.DB_ENV_PATH};" \
                       f"cm_ctl list --param --agent | " \
                       f"grep agent_heartbeat_timeout"
        q_res = self.common.get_sh_result(self.user_nodes[0], q_logdir_cmd)
        self.assertIn('agent_heartbeat_timeout = 5', q_res, f'执行失败: {text}')

        text = '----step2:修改参数值为1;expect:合理报错----'
        self.log.info(text)
        set_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"gs_guc set -Z cmagent -N all -I all -c " \
                  f"agent_heartbeat_timeout=\"1\";"
        set_res = self.common.get_sh_result(self.user_nodes[0], set_cmd)
        expect = 'ERROR: The value 1 is outside the valid range for' \
                 ' parameter "agent_heartbeat_timeout" (2 .. 2147483647)'
        self.assertIn(expect, set_res, f'执行失败: {text}')

        text = '----step3:修改参数值为2147483648;expect:合理报错----'
        self.log.info(text)
        set_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"gs_guc set -Z cmagent -N all -I all -c " \
                  f"agent_heartbeat_timeout=\"2147483648\";"
        set_res = self.common.get_sh_result(self.user_nodes[0], set_cmd)
        expect = 'ERROR: The value 2147483648 is outside the valid range for' \
                 ' parameter "agent_heartbeat_timeout" (2 .. 2147483647)'
        self.assertIn(expect, set_res, f'执行失败: {text}')

        text = '----step4:修改参数值为空;expect:合理报错----'
        self.log.info(text)
        set_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"gs_guc set -Z cmagent -N all -I all -c " \
                  f"agent_heartbeat_timeout=\"''\";"
        set_res = self.common.get_sh_result(self.user_nodes[0], set_cmd)
        expect = 'ERROR: The parameter "agent_heartbeat_timeout" requires ' \
                 'an integer value'
        self.assertIn(expect, set_res, f'执行失败: {text}')

        text = '----step5:修改参数值为null;expect:合理报错----'
        self.log.info(text)
        set_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"gs_guc set -Z cmagent -N all -I all -c " \
                  f"agent_heartbeat_timeout=\"null\";"
        set_res = self.common.get_sh_result(self.user_nodes[0], set_cmd)
        self.assertIn(expect, set_res, f'执行失败: {text}')

    def tearDown(self):
        text = '----poststep1:卸载cm;expect:执行成功----'
        self.log.info(text)
        self.un_cm = CmUnInstall(self.install_pkg_path,
                                 self.pri_sh.node.ssh_user)
        res = self.un_cm.cm_uninstall(
            self.user_nodes[0], '--deleteData --deleteBinary')
        self.assertIn('Uninstall CM tool success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
